package com.xdl.backstage.base.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xdl.backstage.base.bean.UserManagerBean;
import com.xdl.backstage.base.dao.UserManagerDao;
import com.xdl.backstage.base.domain.Company;
import com.xdl.backstage.base.domain.UserManager;
import com.xdl.backstage.base.entity.CompanyEntity;
import com.xdl.backstage.base.entity.UserManagerEntity;
import com.xdl.backstage.base.service.iface.CompanyService;
import com.xdl.backstage.base.service.iface.UserManagerService;
import com.xdl.moudle.utils.util.EnumUtil.OpearteMode;

@Service("UserManagerService")
public class UserManagerServiceImpl  implements UserManagerService{

	@Autowired
	private UserManagerDao userManagerDao;
	@Autowired
	private CompanyService companyService;
	
	@Override
	public UserManagerEntity getData(String id) {
		if (StringUtils.isNotBlank(id)) {
			UserManager data = userManagerDao.selectByPrimaryKey(id);
			if (data != null) {
				UserManagerEntity entity = new UserManagerEntity(data);
				CompanyEntity companyEntity = companyService.getData(data.getCompanyId());
				entity.setCompany(companyEntity);
				return entity;
			}
		}
		return null;
	}

	@Override
	public List<UserManagerEntity> queryDataByParams() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean save(UserManagerBean[] arrUserManager) {
		boolean isSuccess = false;
		if (arrUserManager != null) {
			for (UserManagerBean userManagerBean : arrUserManager) {
				UserManager data = userManagerBean.beanToData();
				if (userManagerBean.getOpearteMode().getStatus() == OpearteMode.NEW.getStatus()) {
					isSuccess = userManagerDao.insertSelective(data) > 0;
				}else if (userManagerBean.getOpearteMode().getStatus() == OpearteMode.MODIFED.getStatus()) {
					isSuccess =  userManagerDao.updateByPrimaryKeySelective(data) > 0;
				}else if (userManagerBean.getOpearteMode().getStatus() == OpearteMode.DELETED.getStatus()) {
					isSuccess = userManagerDao.deleteByPrimaryKey(data.getId()) > 0;
				}
			}
		}
		return isSuccess;
	}

	@Override
	public boolean delete(String id) {
		return userManagerDao.deleteByPrimaryKey(id) > 0;
	}

}
